package com.lichuang.fruitvarietysm.goods.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lichuang.fruitvarietysm.goods.entity.Goods;
import com.lichuang.fruitvarietysm.goods.entity.GoodsEvaluate;
import com.lichuang.fruitvarietysm.goods.entity.GoodsEvalutateAppend;
import com.lichuang.fruitvarietysm.goods.entity.GoodsSkuAttr;
import com.lichuang.fruitvarietysm.goods.mapper.GoodsEvaluateMapper;
import com.lichuang.fruitvarietysm.goods.mapper.GoodsEvalutateAppendMapper;
import com.lichuang.fruitvarietysm.goods.mapper.GoodsMapper;
import com.lichuang.fruitvarietysm.goods.mapper.GoodsskuMapper;
import com.lichuang.fruitvarietysm.goods.service.impl.GoodsEvaluateServiceImpl;
import com.lichuang.fruitvarietysm.goods.service.impl.GoodsSkuAttrServiceImpl;
import com.lichuang.fruitvarietysm.goods.service.impl.GoodsskuServiceImpl;
import com.lichuang.fruitvarietysm.goods.vo.EvaluateAndAppend;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 商品评价表 前端控制器
 * </p>
 *
 * @author shuaihang
 * @since 2019-06-16
 */
@RestController
@RequestMapping("//goods-evaluate")
public class GoodsEvaluateController {

    @Autowired
    GoodsEvaluateServiceImpl goodsEvaluateService;

    @Autowired
    GoodsskuServiceImpl goodsskuService;

    @Autowired
    public GoodsSkuAttrServiceImpl goodsSkuAttrService;

    @Autowired
    GoodsEvaluateMapper goodsEvaluateMapper;

    @Autowired
    GoodsEvalutateAppendMapper appendMapper;

    @Autowired
    GoodsMapper goodsMapper;


    @Autowired
    GoodsskuMapper goodsskuMapper;

    GoodsEvaluate goodsEvaluate=new GoodsEvaluate();
    GoodsEvalutateAppend evalutateAppend=new GoodsEvalutateAppend();

    //当点击添加商品评价时候，跳转页面时候需要查询商品的信息，进行显示。

    //添加商品评价
    @RequestMapping("/addGoodsEvaluate")
    public Object addEvaluate(GoodsEvaluate goodsEvaluate){
        String state;
       int num= goodsEvaluateMapper.insert(goodsEvaluate);
       if(num==1){
           state="发布评论成功";
       }else
       {
           state="发布评价失败,稍后再试";
       }
       return state;
    }

    //通过用户id，可以查到该用户都评价过什么商品
    @RequestMapping("/getUserEvaluateGoods")
    public Object getUserEvaluateGoods(Model model,@RequestParam("user_id")Integer user_id){
        //通过用户id，可以查到该用户都评价过什么商品
        goodsEvaluate.setUserId(user_id);
        QueryWrapper wrapper=new QueryWrapper(goodsEvaluate);
       List<GoodsEvaluate> list= goodsEvaluateMapper.selectList(wrapper);
       List<Goods> goodsList=new ArrayList();
       //循环查找商品的具体信息
        for (GoodsEvaluate evaluate : list) {
           Goods goods= goodsMapper.selectById(evaluate.getGoodsId());
            //需要添加个评价表的id
           goodsList.add(goods);
        }
        model.addAttribute(list);
        return goodsList;
    }


    /**
     * 查找某商品的全部评价
     * @param goods_id
     * @return
     */
    @RequestMapping("/getGoodsEvaluate")
    public  Object getGoodsEvaluate(@RequestParam("goods_id")Integer goods_id,Integer state){
       goodsEvaluate.setEvaluateId(goods_id);
        QueryWrapper wrapper=new QueryWrapper(goodsEvaluate);
        //  根据商品id，查某商品的评价
        List<GoodsEvaluate> list=goodsEvaluateMapper.selectList(wrapper);
  //      List<EvaluateAndAppend> evaluateAndAppendsList=new ArrayList<>();
        //状态等于1，就是只显示追加评论。等于2就显示评价有图片的，默认显示全部
//        if(state==1){
//            evaluateAndAppendsList=goodsEvaluateService.getEvaluateAppend(goods_id);
//        }else if(state==2){
        for (GoodsEvaluate evaluate : list) {
            evalutateAppend.setEvaluateId(evaluate.getEvaluateId());
            QueryWrapper wrapper1=new QueryWrapper(evalutateAppend);
            //根据EvaluateId，查追加的id一一对应
            List<GoodsEvalutateAppend> appendList=appendMapper.selectList(wrapper1);
            //将值添加再对象中，
            evaluate.setAppendList(appendList);
        }
        //判读商品是否存在sku
        int yes=goodsskuMapper.sCountGoodSku(goods_id);
        if(yes!=0){
            for (GoodsEvaluate evaluate : list){

                List<GoodsSkuAttr> attrList=goodsSkuAttrService.sSkuAttribute(goods_id,-1);
                evaluate.setSkuKey(attrList);
                int skuId=goodsEvaluateService.getOrderSkuId(evaluate.getOrderNo());
                String skuValue= goodsEvaluateService.getSkuValue(skuId);
                String[] split = skuValue.split(":");
                List<GoodsSkuAttr> skuAttrsList=new ArrayList<>();
                for (String s : split) {
                    int sku = Integer.parseInt(s);
                    skuAttrsList.add(goodsskuService.sAttName(sku));
                }
                evaluate.setSkuAttrs(skuAttrsList);
            }
        }
        for (GoodsEvaluate evaluate : list) {
            //查该评论对应的用户名
            String userName=goodsEvaluateService.getUserName(evaluate.getUserId());
            evaluate.setUserName(userName);
        }

        return list;
    }

    @RequestMapping("/goodsEvaluateAppend")
    public Object getEvaluateAppend(@RequestParam("goods_id")Integer goods_id,Integer state){
        List<EvaluateAndAppend> evaluateAndAppendsList=goodsEvaluateService.getEvaluateAppend(goods_id);
        //判读商品是否存在sku
        int yes=goodsskuMapper.sCountGoodSku(goods_id);
        if(yes!=0){
            for (EvaluateAndAppend evaluate : evaluateAndAppendsList) {
                List<GoodsSkuAttr> attrList=goodsSkuAttrService.sSkuAttribute(goods_id,-1);
                evaluate.setSkuKey(attrList);
                int skuId=goodsEvaluateService.getOrderSkuId(evaluate.getOrderNo());
                String skuValue= goodsEvaluateService.getSkuValue(skuId);
                String[] split = skuValue.split(":");
                List<GoodsSkuAttr> skuAttrsList=new ArrayList<>();
                for (String s : split) {
                    int sku = Integer.parseInt(s);
                    skuAttrsList.add(goodsskuService.sAttName(sku));
                }
                evaluate.setSkuAttrs(skuAttrsList);
            }
        }
        for (EvaluateAndAppend evaluate : evaluateAndAppendsList) {
            //查该评论对应的用户名
            String userName=goodsEvaluateService.getUserName(evaluate.getUserId());
            evaluate.setUserName(userName);
        }

        return evaluateAndAppendsList;
    }
}
